﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Seesmic.HelloTipi.Controls
{
    public partial class NewAccountControl : UserControl
    {
        public static readonly DependencyProperty AccountProperty = DependencyProperty.Register("HelloTipiAccount", typeof(HelloTipiAccount), typeof(NewAccountControl), new PropertyMetadata(new PropertyChangedCallback(NewAccountControl.OnAccountChanged)));

        public NewAccountControl()
        {
            InitializeComponent();
        }
        
        private void OnAccountChanged()
        {
            this.passwordBox.Password = (this.Account != null) ? (this.Account.Password ?? string.Empty) : string.Empty;
        }

        private static void OnAccountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((NewAccountControl)d).OnAccountChanged();
        }

        private void PasswordBox_KeyUp(object sender, KeyEventArgs e)
        {
            PasswordBox box = (PasswordBox)sender;
            HelloTipiAccount dataContext = (HelloTipiAccount)base.DataContext;
            if (dataContext != null)
            {
                dataContext.Password = box.Password;
            }
        }

        public HelloTipiAccount Account
        {
            get
            {
                return (HelloTipiAccount)base.GetValue(AccountProperty);
            }
            set
            {
                base.SetValue(AccountProperty, value);
            }
        }
    }
}
